//
//  StudyCardTopupSuccViewController.m
//  xpkc
//
//  Created by 胡志飞 on 15/12/3.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "StudyCardTopupSuccViewController.h"

#import "UserMessage.h"

@interface StudyCardTopupSuccViewController ()
{
    NSInteger  _boss_account;
    UILabel * _moneyLabel;
}
@end

@implementation StudyCardTopupSuccViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationItem.title = @"学习卡充值";

        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
//     [self createUI];
    [self createUserData];
    // Do any additional setup after loading the view.
}

-(void)createUserData{
    
    NSString *module = @"user";
    NSString *interface = @"userinfo";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
//    NSString *module = @"user";
//    NSString *interface = @"userinfo";
//    
//    NSString *timeString = [GetTime getTime];
//    
//    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
//    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"userid":[UserMessage sharedUserMessage].userMid};

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    // manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    NSLog(@"99999----%@",urlString);
    NSLog(@"-----%@",parameter);
    
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //成功
        NSDictionary * userDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //之后创建视图
        logdebug(@"%@",userDic);
        NSDictionary * infoDic = [userDic objectForKey:@"info"];
            if ([[userDic valueForKey:@"succ"] intValue]) {
                logdebug(@"成功");
                _boss_account = [infoDic[@"boss_account"] integerValue];
                
                

            }
            [self performSelectorOnMainThread:@selector(freshData) withObject:nil waitUntilDone:YES];
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
        //失败
        logdebug(@"%@",error.localizedDescription);
    }];

    
}

-(void)freshData{
    _boss_account += _account;
//    _moneyLabel.text = [NSString stringWithFormat:@"您的卡上余额为：￥%ld",_boss_account];
    [self createUI];
}


-(void)createUI{
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(40, 120, SCREEN_WIDTH - 80, 40)];
    
    label.text = @"充值成功";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:30];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    UILabel* moneyLabel = [[UILabel alloc] init];
    moneyLabel.frame = CGRectMake(60, SCREEN_HEIGHT * 0.47, SCREEN_WIDTH - 120, 20);
    moneyLabel.textColor = [UIColor redColor];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.font = [UIFont systemFontOfSize:20];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"您的卡上余额为:￥%ld",_boss_account]];
    NSRange redRange = NSMakeRange(0,[[noteStr string] rangeOfString:@":"].location +1);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:redRange];
    [moneyLabel setAttributedText:noteStr] ;
    [moneyLabel sizeToFit];
    [self.view addSubview:moneyLabel];
    
//    UILabel * moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, SCREEN_HEIGHT * 0.45, SCREEN_WIDTH - 80, 20)];
//    moneyLabel.font = [UIFont systemFontOfSize:20];
//    NSString * str = [NSString stringWithFormat:@"您的卡上余额为:￥%ld",_boss_account];
//    NSMutableAttributedString * accountStr = [[NSMutableAttributedString alloc] initWithAttributedString:str];
//    // moneyLabel.text = [NSString stringWithFormat:@"您的卡上余额为:￥%ld",_boss_account];
//   NSRange redRange = NSMakeRange([[accountStr string] rangeOfString:@":"].location, accountStr.length - 1);
//    [accountStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
//    [moneyLabel setAttributedText:accountStr];
//    [moneyLabel sizeToFit];
//    moneyLabel.textAlignment = NSTextAlignmentCenter;
//    _moneyLabel = moneyLabel;
//    [self.view addSubview:moneyLabel];
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(20, SCREEN_HEIGHT * 0.6, SCREEN_WIDTH - 40, 60)];
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:25];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
}

-(void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
